package presentation;

import java.awt.Color;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.print.PrinterException;
import javax.swing.JOptionPane;
import javax.swing.table.TableModel;
import org.jdesktop.swingx.JXTable;
import presentation.entities.DialogCreator;

public class TableFrame extends javax.swing.JFrame {

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        table_scrollPane = new javax.swing.JScrollPane();
        menuBar = new javax.swing.JMenuBar();
        tools_menu = new javax.swing.JMenu();
        print_menuItem = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        close_menuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setIconImage(Toolkit.getDefaultToolkit().getImage(PresentationConfig.tableIconPath));

        menuBar.setMaximumSize(new java.awt.Dimension(75, 32769));

        tools_menu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/tools-icon.png"))); // NOI18N
        tools_menu.setText("Tools");
        tools_menu.setFont(new java.awt.Font("Segoe UI", 1, 12));

        print_menuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.SHIFT_MASK));
        print_menuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/print-icon.png"))); // NOI18N
        print_menuItem.setText("Print");
        print_menuItem.setToolTipText("Prints the table. Make sure that all the relevant columns are packed.");
        print_menuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                print_menuItemActionPerformed(evt);
            }
        });
        tools_menu.add(print_menuItem);
        tools_menu.add(jSeparator1);

        close_menuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.SHIFT_MASK));
        close_menuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/shutdown-icon.png"))); // NOI18N
        close_menuItem.setText("Close");
        close_menuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                close_menuItemActionPerformed(evt);
            }
        });
        tools_menu.add(close_menuItem);

        menuBar.add(tools_menu);

        setJMenuBar(menuBar);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(table_scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, table_scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void close_menuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_close_menuItemActionPerformed

        this.dispose();     }//GEN-LAST:event_close_menuItemActionPerformed

    private void print_menuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_print_menuItemActionPerformed

        try {             table.print();         } catch (PrinterException ex) {             DialogCreator.showMessage(this, "Print Error", ex.getMessage(), JOptionPane.ERROR_MESSAGE);         }     }//GEN-LAST:event_print_menuItemActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem close_menuItem;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem print_menuItem;
    private javax.swing.JScrollPane table_scrollPane;
    private javax.swing.JMenu tools_menu;
    // End of variables declaration//GEN-END:variables
    private JXTable table;

    /** Creates new form TableFrame */
    public TableFrame(String title, TableModel model) {
        super(title);
        initComponents();
        initTable(model);
    }
    
    private void initTable(TableModel model) {
        table = new JXTable(model);
        table.setFont(new Font("Ariel", Font.PLAIN, 12));
        table.getTableHeader().setFont(new Font("Dialog", Font.BOLD, 13));
        table.setColumnControlVisible(true);
        table.setShowVerticalLines(true);
        table.setShowHorizontalLines(true);
        table.setHorizontalScrollEnabled(true);
        table.setColumnMargin(1);
        table.setGridColor(Color.GRAY);
        table_scrollPane.setViewportView(table);
        table.setSortable(true);
    }  
}
